package ace.cmp.data.methodname.sql.mybatisplus.core.mysql;

import ace.cmp.data.methodname.method.name.sql.MethodSqlBuilder;
import ace.cmp.data.methodname.method.name.sql.core.util.StringUtils;
import ace.cmp.data.methodname.method.name.sql.entity.resolver.EntityMethodNameToQueryResolver;
import ace.cmp.data.methodname.sql.mybatisplus.core.mysql.dml.*;
import ace.cmp.data.methodname.sql.mybatisplus.core.properties.MybatisPlugMethodNameToSqlProperties;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author caspar
 * @date 2023/2/24 9:05 mybatis plus 根据method推导sql
 */
public class MethodToSqlMysqlBuilder implements MethodSqlBuilder {
  private static final Logger log = LoggerFactory.getLogger(MethodToSqlMysqlBuilder.class);
  private final EntityMethodNameToQueryResolver queryResolver;
  private final TableInfo tableInfo;
  private final List<AbstractMethodToDmlSqlBuilder> dmlSqlBuilder;

  @Override
  public String getSql() {

    return dmlSqlBuilder.stream()
        .filter(p -> p.getSupport())
        .map(p -> p.getSql())
        .findFirst()
        .orElse(StringUtils.EMPTY);
  }

  public MethodToSqlMysqlBuilder(
      EntityMethodNameToQueryResolver queryResolver,
      TableInfo tableInfo,
      MybatisPlugMethodNameToSqlProperties sqlProperties) {
    this.queryResolver = queryResolver;
    this.tableInfo = tableInfo;
    this.dmlSqlBuilder =
        Arrays.asList(
            new MethodToSqlSelectMysqlBuilder(queryResolver, tableInfo, sqlProperties),
            new MethodToSqlCountMysqlBuilder(queryResolver, tableInfo, sqlProperties),
            new MethodToSqlExistsMysqlBuilder(queryResolver, tableInfo, sqlProperties),
            new MethodToSqlDeleteMysqlBuilder(queryResolver, tableInfo, sqlProperties),
            new MethodToSqlNoneMysqlBuilder(queryResolver, tableInfo, sqlProperties));
  }
}
